<template>
   <div class="app-container">
      <div class="head-container">
         <a-form :model="queryParams" ref="queryRef" layout="inline" laba-width="68px">
            <a-form-item prop="dictName">
               <a-input v-model="queryParams.dictName" placeholder="请输入字典名称" clearable style="width: 240px"
                  @keyup.enter="handleQuery" />
            </a-form-item>
            <a-form-item prop="dictType">
               <a-input v-model="queryParams.dictType" placeholder="请输入字典类型" clearable style="width: 240px"
                  @keyup.enter="handleQuery" />
            </a-form-item>
            <a-form-item prop="status">
               <a-select v-model="queryParams.status" placeholder="字典状态" clearable style="width: 240px">
                  <a-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label"
                     :value="dict.value" />
               </a-select>
            </a-form-item>
            <a-form-item>
               <a-space>
                  <a-button type="primary" @click="handleQuery">
                     <template #icon>
                        <icon-search />
                     </template>
                     搜索</a-button>
                  <a-button @click="resetQuery">
                     <template #icon>
                        <icon-refresh />
                     </template>
                     重置</a-button>
               </a-space>
            </a-form-item>
         </a-form>
      </div>
      <div class="main-container">
         <div class="main-toolbar">
            <a-space>
               <a-button type="primary" @click="showAddDict">
                  <template #icon>
                     <icon-plus />
                  </template>
                  新增</a-button>
               <a-button status="success" :disabled="selectedKeys.length !== 1" @click="handleUpdate">
                  <template #icon>
                     <icon-edit />
                  </template>
                  修改</a-button>
               <a-button status="danger" :disabled="selectedKeys.length === 0" @click="handleDelete">
                  <template #icon>
                     <icon-delete />
                  </template>
                  删除</a-button>
               <a-button status="danger" @click="handleRefreshCache">
                  <template #icon>
                     <icon-refresh />
                  </template>
                  刷新缓存</a-button>
            </a-space>
         </div>
         <div class="main-content">
            <a-table :columns="columns" :data="typeList" row-key="dictId"
               :row-selection="{ type: 'checkbox', showCheckedAll: true }" @selection-change="handleSelectionChange">

            </a-table>
         </div>
      </div>
      <DictForm ref="dictFormRef"></DictForm>
   </div>
</template>

<script setup name="Dict">
import { addType, delType, getType, listType, refreshCache, updateType } from "@/api/system/dict/type";
import useDictStore from '@/stores/dict';
import DictForm from "./dictForm.vue";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const typeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      dictName: undefined,
      dictType: undefined,
      status: undefined
   },
   rules: {
      dictName: [{ required: true, message: "字典名称不能为空", trigger: "blur" }],
      dictType: [{ required: true, message: "字典类型不能为空", trigger: "blur" }]
   },
});

const { queryParams, form, rules } = toRefs(data);

const columns = ref([
   { key: 0, title: '字典编号', dataIndex: 'dictId' },
   { key: 1, title: '字典名称', dataIndex: 'dictName' },
   { key: 2, title: '字典类型', dataIndex: 'dictType' },
   { key: 3, title: '状态', dataIndex: 'status' },
   { key: 4, title: '备注', dataIndex: 'remark' },
   { key: 5, title: '创建时间', dataIndex: 'createTime' },
   { key: 6, title: '操作', slotName: 'action' }
])
const selectedKeys = ref([])
/** 查询字典类型列表 */
function getList() {
   loading.value = true;
   listType(proxy.addDateRange(queryParams.value, dateRange.value)).then(({ data }) => {
      typeList.value = data.rows;
      total.value = data.total;
      loading.value = false;
   });
}
/** 取消按钮 */
function cancel() {
   open.value = false;
   reset();
}
/** 表单重置 */
function reset() {
   form.value = {
      dictId: undefined,
      dictName: undefined,
      dictType: undefined,
      status: "0",
      remark: undefined
   };
   proxy.resetForm("dictRef");
}
/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   handleQuery();
}
/** 新增按钮操作 */
function showAddDict() {
   proxy.$refs.dictFormRef.init('add')
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
   selectedKeys.value = selection
}
/** 修改按钮操作 */
function handleUpdate(row) {
   reset();
   const dictId = row.dictId || ids.value;
   getType(dictId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改字典类型";
   });
}
/** 提交按钮 */
function submitForm() {
   proxy.$refs["dictRef"].validate(valid => {
      if (valid) {
         if (form.value.dictId != undefined) {
            updateType(form.value).then(response => {
               proxy.$modal.msgSuccess("修改成功");
               open.value = false;
               getList();
            });
         } else {
            addType(form.value).then(response => {
               proxy.$modal.msgSuccess("新增成功");
               open.value = false;
               getList();
            });
         }
      }
   });
}
/** 删除按钮操作 */
function handleDelete(row) {
   const dictIds = row.dictId || ids.value;
   proxy.$modal.confirm('是否确认删除字典编号为"' + dictIds + '"的数据项？').then(function () {
      return delType(dictIds);
   }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
   }).catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
   proxy.download("system/dict/type/export", {
      ...queryParams.value
   }, `dict_${new Date().getTime()}.xlsx`);
}
/** 刷新缓存按钮操作 */
function handleRefreshCache() {
   refreshCache().then(() => {
      proxy.$modal.msgSuccess("刷新成功");
      useDictStore().cleanDict();
   });
}

getList();
</script>
